#SRS ESTIMATES:
model_iv = iv_robust(Y ~ C | T, data=df_all, try_cholesky=TRUE, se_type="HC1")
model_iv = iv_robust(Y ~ C+turf | T+turf, data=df_all, try_cholesky=TRUE, se_type="HC1")

model_psw = glm(C~(age+age2+age35+voted00+primary+
    missing_primary+famsize)*city, data=df_all[df_all$T==1,], family='binomial')
scores = predict(model_psw, df_all[df_all$T==1,], type='response')
var(scores)/var(df_all$C[df_all$T==1])
C_hat = ifelse(predict(model_psw, df_all[df_all$T==1,], 'response') > 0.5, 1, 0)
table(C_hat, df_all$C[df_all$T==1])
#MATCHING:
#Plug in data = df_all


cities = unique(df_all$city)
#set.seed(10)
set.seed(10)
block_set1<-lapply(1:6, function(x){
    if(cities[x]=="Bridgeport"){
        blocking_set1 = c("age", "age2", "age35", "voted00")
    }
    if(cities[x] == "Raleigh"){
        blocking_set1 = c("primary", "voted99", "age2", "age", "turf")
    }
    if(cities[x] == "Minneapolis"){
        blocking_set1 = c("primary", "voted00", "age2", "age")
    }
    if(cities[x] == "Detroit"){
        blocking_set1 = c("primary", "voted99", "voted00", "age")
    }
    if(cities[x] == "Columbus"){
        blocking_set1 = c("voted99", "primary")
    }
    if(cities[x] == "St. Paul"){
        blocking_set1 = c("age", "age2", "primary", "voted99")
    }
    return(
        estimate_matches(block_vars= blocking_set1, data = df_all[which(df_all$city==cities[x]),])
    )
    })

#set.seed(2)
set.seed(10)
block_set2<-lapply(1:6, function(x){
    if(cities[x]=="Bridgeport"){
        blocking_set2 = c("voted00", "age")
    }
    if(cities[x] == "Raleigh"){
        blocking_set2 = c("primary", "turf")
    }
    if(cities[x] == "Minneapolis"){
        blocking_set2 = c("primary", "age2")
    }
    if(cities[x] == "Detroit"){
        blocking_set2 = c("voted99", "primary")
    }
    if(cities[x] == "Columbus"){
        blocking_set2 = c("voted99", "primary")
    }
    if(cities[x] == "St. Paul"){
        blocking_set2 = c("age", "primary")
    }
    return(
        estimate_matches(block_vars= blocking_set2, data = df_all[which(df_all$city==cities[x]),])
    )
    })
save(block_set1, block_set2, file="Data/generated/results_matching.Rdata")